//
//  DicForumViewController.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/8/1.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "DicForumViewController.h"
#import "ForumViewModel.h"
@interface DicForumViewController ()<UITextViewDelegate>
{
    UITextView* discussText;
    UIButton* sure;
    UILabel* PlaceHolderLable;
    
}
@property (nonatomic,strong) ForumViewModel * forumViewModel;

@end

@implementation DicForumViewController

-(ForumViewModel *)forumViewModel{
    
    if (!_forumViewModel) {
        _forumViewModel = [[ForumViewModel alloc] init];
    }
    return _forumViewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initUI];
    // Do any additional setup after loading the view.
}

-(void)initNav{
    
    [self.navigationItem setTitle:@"评论"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
}

-(void)initUI{
    
    discussText=[UITextView new];
    discussText.layer.cornerRadius = 5;
    discussText.layer.masksToBounds = YES;
    discussText.textColor = baseBlackColor;
    discussText.font = DEF_MyFont(16);
    discussText.delegate=self;
    
    discussText.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15);
    discussText.backgroundColor= DEF_COLOR_F0F0F0;
    [self.view addSubview:discussText];
    [discussText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-30);
        make.height.mas_equalTo(180);
        make.top.equalTo(self.view).with.offset(20);
        make.leading.equalTo(self.view).with.offset(15);
    }];
    
    PlaceHolderLable = [[UILabel alloc]initWithFrame:CGRectMake(19, 15, 200, 20)];
    PlaceHolderLable.enabled = NO;
    PlaceHolderLable.text = @"你的评论举足轻重哟~";
    PlaceHolderLable.font = DEF_MyFont(16);
    PlaceHolderLable.textColor = baseLightBlackColor;
    [discussText addSubview:PlaceHolderLable];
    
    //    登录
    sure=[UIButton new];
    [sure setTitle:@"确定" forState:UIControlStateNormal];
    [sure setTitleColor:baseWhiteColor forState:UIControlStateNormal];
    [sure addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    sure.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    sure.backgroundColor=DEF_COLOR_MAINCOLOR;
    sure.layer.cornerRadius = 5.f;
    [self.view addSubview:sure];
    [sure mas_makeConstraints:^(MASConstraintMaker * make){
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-80);
        make.height.mas_equalTo(40);
        make.top.equalTo(discussText.mas_bottom).with.offset(40);
        make.leading.equalTo(self.view).with.offset(40);
    }];
    
}

-(void)sure:(UIButton* )sender{
    
    
    if ([discussText.text isEqualToString:@""]) {
        
        [MXUtility showTips:@"评论不能为空哦~"];
        
    }else{
        
        [MXUtility showMBProgress:self.view message:LoadingText];
        
        [[self.forumViewModel addForumDiscussRequestParameter:self.forumID content:discussText.text userId:UserInfoShare.userCenterModel.userID] subscribeNext:^(id result) {
            
            if([result[@"errorcode"] integerValue] == 0){
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"评论成功" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *Sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
                    
                    if (self.backBlock) {
                        self.backBlock();
                    }
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }];
                
                [alert addAction:Sure];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
            [MXUtility hideMBProgress:self.view];
        }error:^(NSError *error) {
            
            [MXUtility hideMBProgress:self.view];
            
        }];
        
        
    }
    
    
    
}

-(void)textViewDidChange:(UITextView *)textView{
    
    if ([discussText.text length] == 0) {
        
        [PlaceHolderLable setHidden:NO];
        
    }else
        
    {
        
        [PlaceHolderLable setHidden:YES];
        
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [discussText resignFirstResponder];
    
}

-(NSString *)base64EncodeString:(NSString *)string
{
    
    //1.先把字符串转换为二进制数据
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    //2.对二进制数据进行base64编码，返回编码后的字符串
    return [data base64EncodedStringWithOptions:0];
    
}

@end
